/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.statementView;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.ViewSupplierReturnSummary;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonPrint;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.ViewCommodityPurchaseReturnModel;
import com.ruipengkj.service.ViewPurchaseCommodityReturnSummaryService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.purchaseView.PurchaseRetreatAddPanel;

/**
 * 供应商退货汇总表
 */
public class StatementSupplierReturnSummary extends BasePagePanel<ViewSupplierReturnSummary> implements Runnable {
	private static final long serialVersionUID = 1L;
	private DataPackageComponent startTime;//开始日期
	private DataPackageComponent endTime; //结束日期
	
	private JButton selectButton;//选择
	private JButton clearButton;//清除按钮
//	private JButton printButton; //打印
	private JButton exportButton; //导出
//	private JButton exitButton; //退出
	
	private JTable table;//商品销售汇总表
	private JScrollPane tableJsp;//
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<ViewSupplierReturnSummary> pageView;//翻页主键
	
	private ViewCommodityPurchaseReturnModel tableModel;
//	private TBillDetailService tBillDetailService;
	
	private ViewPurchaseCommodityReturnSummaryService viewCommodityPurchaseReturnSummaryService;
	public StatementSupplierReturnSummary() {
//		tBillDetailService = BuildSpringApplication.getInstance().getTBillDetailServiceImpl();
		viewCommodityPurchaseReturnSummaryService = BuildSpringApplication.getInstance().getViewCommodityPurchaseReturnSummaryServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
	}
	
	private void init() {
		pageView=new PageView<ViewSupplierReturnSummary>();
		tableModel = new ViewCommodityPurchaseReturnModel();
		table = uiFactory.createRPJTable(tableModel, null, this, 30, JLabel.CENTER);
		tableJsp = new JScrollPane(table);
		this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(this.tailPanel(), BorderLayout.SOUTH);
		initListener();
		
		// 排序
		TableRowSorter<ViewCommodityPurchaseReturnModel> tableRowSorter = new TableRowSorter<ViewCommodityPurchaseReturnModel>(
				tableModel);
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel() {
		JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		startTime = new DataPackageComponent("开始日期", 15);
		endTime = new DataPackageComponent("结束日期", 15);
		selectButton = new RPJButtonBlue("查询");	
		clearButton=new RPJButtonGray("清空");
		
		jp1.add(startTime);
		jp1.add(endTime);
		jp1.add(selectButton);
		jp1.add(clearButton);
		return jp1;
	}
	
	private JPanel tailPanel() {
		JPanel tail_panel = new JPanel(new GridLayout(2, 1));
		JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
//		printButton = new JButton("打印");
		exportButton = new RPJButtonPrint("导 出");
//		exitButton = new JButton("退出");
//		jp2.add(printButton);
		jp2.add(exportButton);
//		jp2.add(exitButton);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		tail_panel.add(pagingComponent);
		tail_panel.add(jp2);
		return tail_panel;
	}
	
	private void initListener(){
		selectButton.addActionListener(this);
		exportButton.addActionListener(this);
		clearButton.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == selectButton){
			this.termQuery(1);
		} else if(e.getSource()==exportButton){
			//导出
			FileDialog fileDialog = new FileDialog(new JDialog(), "文件导出",FileDialog.SAVE);
			fileDialog.setVisible(true);
			String fileDir = fileDialog.getDirectory();
			String fileName = fileDialog.getFile();
			if (fileDir != null && !fileDir.equals("") && fileName != null&& !fileName.equals("")) {
				if (!fileName.toLowerCase().endsWith(".xls")) {
					if (fileName.lastIndexOf(".") != -1) {
						fileName = fileName.substring(0, fileName.lastIndexOf("."))+ ".xls";
					} else {
						fileName = fileName + ".xls";
					}
				}
//				try {
//					List<ViewSupplierReturnSummary> list = viewCommodityPurchaseReturnSummaryService.findExportService(this.queryTerm());
//					if (list != null && list.size() != 0) {
//						exportButton.setEnabled(false);
//						CommodityPurchaseReturnExportUtil.writeExcel(fileDir + fileName,list);
//						JOptionPane.showMessageDialog(null, "导出成功");
//					} else {
//						JOptionPane.showMessageDialog(null, "数据为空，导出失败");
//					}
//				} catch (Exception e1) {
//					e1.printStackTrace();
//					JOptionPane.showMessageDialog(null, "导出失败");
//				}
//				exportButton.setEnabled(true);
			}
		} else if (e.getSource() == clearButton) {
			startTime.setDate(null); //时间1
			endTime.setDate(null); //时间2
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == table && e.getClickCount() == 2) {
			PurchaseRetreatAddPanel purchaseRetreatAddPanel = new PurchaseRetreatAddPanel(null, "采购退货单详细", true);
			purchaseRetreatAddPanel.showDialog((String)table.getValueAt(table.getSelectedRow(), 0));
		}
	}
	
	private ViewSupplierReturnSummary queryTerm(){
		ViewSupplierReturnSummary viewCommodityPurchaseReturnSummary=new ViewSupplierReturnSummary();
		return viewCommodityPurchaseReturnSummary;
	}
	@Override
	protected void termQuery(final int currentPage){
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
//				try {
//					pageView.setCurrentPage(currentPage);
//					PageModel<ViewSupplierReturnSummary> pageModel = viewCommodityPurchaseReturnSummaryService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),queryTerm());
//					pageView.setPageModel(pageModel);
//					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
//					if(pageModel!=null){
//						tableModel.setRowData(pageModel.getResultList());
//						SwingUtilities.invokeLater(new Runnable() {
//							@Override
//							public void run() {
//								tableJsp.getViewport().remove(waitComponent);
//								tableJsp.getViewport().add(table);
//								Util.setJTabelAutoClumnWidth(table);
//								pagingComponent.show();
//							}
//						});
//					}else{
//						//没有订单数据
//						waitComponent.setText("不存在数据");
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
			}
		}.start();
	}
	@Override
	protected PageView<ViewSupplierReturnSummary> getPageView() {
		return pageView;
	}
	@Override
	public void run() {
		this.termQuery(1);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
